<script>
import { reactive, onBeforeMount } from "vue";
import { useRouter } from "vue-router";
// 引入接口
import { getBookcabinetJsonData } from "../assets/api/bookcabinet.js";
export default {
  setup() {
    let router = useRouter();

    // 返回上一页
    let black = () => {
      router.go(-1);
    };

    let listObj = reactive({
      allList: [],
      searchItem: [],
      text: "",
      resultItem: [],
      flag: false, //没有数据
      itemShow: true, //历史搜索页面
      historyItem: [],
    });

    // 数据初始化
    let dateInit = async () => {
      let data = await getBookcabinetJsonData();
      listObj.allList = JSON.parse(JSON.stringify(data.allList));
      listObj.searchItem = data.allList.splice(0, 10);

      listObj.historyItem = JSON.parse(window.localStorage.getItem("history"));
    };

    // 挂载前
    onBeforeMount(() => {
      // 数据初始化
      dateInit();
    });

    // 点击进入详情页
    let details = (id) => {
      router.push({
        path: "/Details",
        query: { id: id },
      });
    };

    // 搜索
    let changFun = (e) => {
      listObj.text = e.target.value;
    };

    // 点击搜索
    let likeSearch = () => {
      listObj.resultItem = listObj.allList.filter((item) => {
        if (listObj.text) {
          return item.BookName.includes(listObj.text);
        }
      });

      if (!listObj.resultItem || listObj.resultItem.length == 0) {
        listObj.flag = true;
      } else {
        listObj.flag = false;
      }

      let his = JSON.parse(window.localStorage.getItem("history"));
      if (!his) {
        window.localStorage.setItem("history", JSON.stringify([listObj.text]));
      } else {
        let i = his.every((item) => {
          return item != listObj.text;
        });
        if (i) {
          his.push(listObj.text);
          window.localStorage.setItem("history", JSON.stringify(his));
        }
      }

      listObj.historyItem = JSON.parse(window.localStorage.getItem("history"));

      listObj.itemShow = false;
    };

    // 清空输入框
    let del = () => {
      listObj.text = "";
      listObj.resultItem = [];
      listObj.itemShow = true;
    };

    return {
      black,
      listObj,
      details,
      changFun,
      del, //清空输入框
      likeSearch,
    };
  },
};
</script>

<template>
  <div class="Search">
    <van-sticky>
      <div class="top">
        <span class="iconfont icon-xiangzuo1" @click="black"></span>
        <div class="top_inp">
          <span class="iconfont icon-sousuo"></span>
          <input
            type="text"
            placeholder="快来搜索想看的书吧"
            v-model="listObj.text"
            @change="changFun"
          />
          <span class="cha" v-show="listObj.text" @click="del">×</span>
        </div>
        <p @click="likeSearch">搜索</p>
      </div>
    </van-sticky>

    <div class="box" v-show="listObj.itemShow">
      <div class="history">
        <div class="his_top">
          <p>历史搜索</p>
          <span class="iconfont icon-shanchu"></span>
        </div>
        <ul v-if="listObj.historyItem">
          <li
            @click="listObj.text = item"
            v-for="(item, index) in listObj.historyItem"
            :key="index"
          >
            {{ item }}
          </li>
        </ul>
      </div>
      <div class="history hot">
        <div class="his_top">
          <p>热门搜索</p>
          <span class="iconfont icon-remen1"></span>
        </div>
        <div class="ul">
          <div
            class="li"
            v-for="item in listObj.searchItem"
            :key="item.BookId"
            @click="details(item.BookId)"
          >
            {{ item.BookName }}
          </div>
        </div>
      </div>
    </div>

    <div class="commit" v-show="listObj.resultItem && listObj.text">
      <div class="commit_box">
        <div
          class="commit_item"
          v-for="item in listObj.resultItem"
          :key="item.BookId"
          @click="details(item.BookId)"
        >
          <div class="img">
            <img :src="item.BookImage" alt="" />
          </div>
          <div class="text">
            <p class="title">{{ item.BookName }}</p>
            <p class="content">
              {{ item.Description }}
            </p>
            <div class="bottom">
              <p class="name">{{ item.Author }}</p>
              <p class="cateory">{{ item.tagList[item.tagList.length - 1] }}</p>
            </div>
          </div>
        </div>
        <van-empty description="暂无相关数据" v-show="listObj.flag" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.Search {
  width: 100%;
  min-width: 375px;
  max-width: 750px;
  box-sizing: border-box;
  .top {
    padding: 0 13px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #fff;
    .top_inp {
      height: 32px;
      width: 274px;
      background-color: #f5f5f5;
      border-radius: 7px;
      display: flex;
      align-items: center;
      text-align: center;
      box-sizing: border-box;
      .icon-sousuo {
        font-size: 15px;
        margin-left: 10px;
      }
      input {
        border: none;
        margin-left: 6px;
        &::-webkit-input-placeholder {
          font-size: 14px;
        }
      }
      .cha {
        position: fixed;
        right: 60px;
        font-size: 20px;
        margin-right: 10px;
        padding: 0 4px;
        height: 20px;
        border-radius: 50%;
        line-height: 21px;
        text-align: center;
        background-color: #dfdbdb;
        color: #7c7d80;
      }
    }
    .icon-xiangzuo1 {
      font-size: 20px;
      color: #6c6a69;
    }
    p {
      font-size: 14px;
      color: #6c6a69;
    }
  }
  .history {
    margin-top: 20px;
    padding: 0 16px;
    .his_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
        font-size: 16px;
        font-weight: bold;
      }
      .icon-shanchu,
      .icon-remen1 {
        font-size: 18px;
        color: #7c7d80;
      }
    }
    ul {
      margin-top: 10px;
      display: flex;
      flex-wrap: wrap;
      li {
        font-size: 12px;
        color: #9c929a;
        height: 30px;
        line-height: 30px;
        padding: 0 8px;
        background-color: #f5f5f5;
        margin-right: 10px;
        border-radius: 6px;
        margin-top: 10px;
      }
    }
  }
  .hot {
    margin-top: 100px;
    .ul {
      display: flex;
      flex-wrap: wrap;
      margin-top: 10px;
      .li {
        width: 50%;
        height: 30px;
        line-height: 30px;
        font-size: 14px;
        color: #000;
      }
    }
  }
  .commit {
    margin-top: 20px;
    padding: 0 13px;
    .commit_box {
      margin-top: 10px;
      .commit_item {
        display: flex;
        margin-top: 7px;
        .img {
          width: 110px;
          height: 144px;
          border-radius: 6px;
          overflow: hidden;
          flex-shrink: 0;
        }
        .text {
          display: flex;
          flex-direction: column;
          margin-left: 10px;
          height: 144px;
          justify-content: space-around;
          border-bottom: 1px solid #f7f7f7;
          padding-bottom: 7px;
          .title {
            font-size: 17px;
            font-weight: bold;
          }
          .content {
            color: #a4a8ab;
            font-size: 13px;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .bottom {
            display: flex;
            justify-content: space-between;
            .name {
              font-size: 12px;
              color: #b1b1b1;
            }
            .cateory {
              color: #291939;
              height: 22px;
              line-height: 22px;
              padding: 0 8px;
              text-align: center;
              background-color: #f5f5f5;
              border-radius: 5px;
              font-size: 13px;
            }
          }
        }
      }
    }
  }
}
</style>